/*
 * @Author: DESKTOP-05FP536\Administrator 18368095041@163.com
 * @Date: 2025-01-14 17:20:49
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2025-04-16 08:44:56
 * @FilePath: /vue3-admin/src/service/usePermission.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { useRoute, useRouter } from 'vue-router';

import router from '@/router';
import { usePermissionStore } from '@/store';
import { NO_PERMISSION_REDIRECT_PATH } from '@/constants';

import { useMy } from './useMy';

export const usePermission = () => {
    const { updateUserStoreMyInfo } = useMy();
    const permission = usePermissionStore();
    const { path } = useRoute();

    const updatePermission = async () => {
        await updateUserStoreMyInfo();

        // 更新权限
        await permission.updateRoutes();

        // 判断是否有权查看页面
        if (!permission.hasRouteAuth(path)) {
            router.push(NO_PERMISSION_REDIRECT_PATH);
        }
    };

    return {
        updatePermission
    };
};
